package myky.com.doctorapp.presenter

import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import myky.com.doctorapp.entity.GroupItemEntity
import myky.com.doctorapp.base.BasePresenterIm
import myky.com.doctorapp.base.RxPageSubscribe
import myky.com.doctorapp.contract.FindDoctorByDieseContract
import myky.com.doctorapp.entity.DepatmentWrapperEntity
import myky.com.doctorapp.entity.PageEntity
import myky.com.doctorapp.model.FindDoctorModel
import myky.com.doctorapp.utils.ILog
import myky.com.doctorapp.utils.ProgressTransformer
import java.util.concurrent.TimeUnit
import kotlin.collections.ArrayList

class FindDoctorByDiesePresenter : BasePresenterIm<FindDoctorByDieseContract.View>(), FindDoctorByDieseContract.Presenter {

    private var findDoctorModel = FindDoctorModel()


    override fun findDepartMent() {
        findDoctorModel.findDepartMent()
                .compose(ProgressTransformer(mContext.fragmentManager,"正在加载"))
                .subscribe(object : RxPageSubscribe<DepatmentWrapperEntity.Depatment>() {
            override fun onSuccess(t: DepatmentWrapperEntity.Depatment?, pageEntity: PageEntity?) {

                val groupItems = ArrayList<GroupItemEntity>()

                val departmentEntity = t?.department


                departmentEntity?.let {
                    for ((index, value) in it.withIndex()) {

                        val groupItem = GroupItemEntity()

                        groupItem.title = value.departmentname

                        when (index) {
                            0 -> groupItem.isHasSelected = true
                        }

                        val departmentchild = value.departmentchild

                        val childItems = java.util.ArrayList<GroupItemEntity.ChildItemeEntity>()
                        departmentchild.let { it ->
                            for ((index, value) in it.withIndex()) {

                                val childItemEntity: GroupItemEntity.ChildItemeEntity = GroupItemEntity.ChildItemeEntity()

                                childItemEntity.title = value.departmentchildname

                                when (index) {
                                    0 -> childItemEntity.isHasSelected = true
                                }

                                val diseaselist = value.diseaselist

                                val diseaseItems = ArrayList<GroupItemEntity.ChildItemeEntity.DiseaseItem>()
                                diseaselist.let { it ->

                                    for (value in it) {

                                        val diseaseItemEntity: GroupItemEntity.ChildItemeEntity.DiseaseItem = GroupItemEntity.ChildItemeEntity.DiseaseItem()

                                        diseaseItemEntity.diseaseName = value.diseasename

                                        diseaseItems.add(diseaseItemEntity)

                                    }
                                    childItemEntity.diseaseItems = diseaseItems
                                    childItems.add(childItemEntity)

                                }
                                groupItem.items = childItems

                            }
                        }
                        groupItems.add(groupItem)

                    }
                }

                mView.showDeapartMent(groupItems)
            }

            override fun onFailed(code: Int, msg: String?) {
                mView.showDataError(msg)
            }

            override fun onSubscribe(d: Disposable) {
                addSubscription(d)

            }
        })
    }
}
